package com.example.teamtribe.main.club.clubintroduce.update;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.base.PhotoActivity;
import com.example.teamtribe.common.PopupPics;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.utils.Utility;
import com.jph.takephoto.model.TResult;

import java.io.File;

public class UpdateIntroduceActivity extends PhotoActivity implements UpdateContract.View,View.OnClickListener,PopupPics.OnPickListener{
    private Team team;
    private File head;
    private PopupPics popup;
    private UpdateContract.Presenter presenter;

    private ImageView ivHead;
    private TextView clubname;
    private TextView clubcomment;
    private ImageView done;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_update);

        ivHead=(ImageView)findViewById(R.id.iv_head);
        clubname=(TextView)findViewById(R.id.et_clubname);
        clubcomment=(TextView)findViewById(R.id.et_clubcomment);

        ((TextView)findViewById(R.id.tv_title)).setText("修改介绍");
        done=(ImageView)findViewById(R.id.iv_more);
        done.setImageResource(R.drawable.ic_done);
        done.setVisibility(View.VISIBLE);
        done.setOnClickListener(this);
        ivHead.setOnClickListener(this);

        team= getIntent().getParcelableExtra("team");
        Glide.with(this).load(Utility.getUri(team.getPortrait())).into(ivHead);
        clubname.setText(team.getName());
        clubcomment.setText(team.getComment());
        popup = new PopupPics(this, this);
        presenter = new UpdatePresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_head:
                popup.show(v);
                break;

            case R.id.iv_more:
                presenter.update(head, clubname.getText().toString().trim(),clubcomment.getText().toString().trim(), team.getPortrait(), team.getRealId(),"c16102bfe30021d74d153f07d1008ea8");
                break;

            default:
                break;
        }
    }

    @Override
    public void next() {
        finish();
    }

    @Override
    public void takeSuccess(TResult result) {
        String path;
        if (result.getImage().isCompressed()) {
            path = result.getImage().getCompressPath();
        } else {
            path = result.getImage().getOriginalPath();
        }
        head = new File(path);
        Glide.with(this).load(head).into(ivHead);
    }

    @Override
    public void fromGallery() {
        getTakePhoto().onPickFromGalleryWithCrop(getUri(), getCropOptions());
    }

    @Override
    public void fromCapture() {
        getTakePhoto().onPickFromCaptureWithCrop(getUri(), getCropOptions());
    }
}
